<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\admin\model\goods\Order;
use app\common\controller\Api;
use app\common\controller\Payment;
use think\Controller;
use think\Db;
use think\Request;


class Wanlshopgoodsfollow extends Api
{
    private static $model;
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';


    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        self::$model = new \app\common\model\WanlshopGoodsFollow();
    }

    /**
     * 添加收藏
     */
    public function create()
    {
        $goodsId = input('goods_id');
        if (!$goodsId) {
            $this->error('参数缺失！');
        }

        $where = [
            'user_id' => $this->auth->id,
            'goods_id' => $goodsId,
            'goods_type' => 'score',
        ];

        $row_info = self::$model->where($where)->find();
        if (!$row_info) {
            $pdata = [
                'user_id' => $this->auth->id,
                'goods_id' => $goodsId,
                'goods_type' => 'score',
            ];

            $res = self::$model->create($pdata);
            if (!$res) {
                $this->error('收藏失败！');
            }
            $this->success('收藏成功！');
        } else {
            if ($row_info->deletetime > 0 || $row_info->status == 'hidden') {
                $res = self::$model->where('id', $row_info->id)->update(['deletetime' => 0, 'status' => 'normal']);
                $msg = '添加';
            } else {
                $res = self::$model->where('id', $row_info->id)->update(['deletetime' => time(), 'status' => 'hidden']);
                $msg = '取消';
            }

            if (!$res) {
                $this->error($msg . '收藏失败！');
            }
            $this->success($msg . '收藏成功！');
        }
    }

    /**
     * 是否收藏商品
     */
    public function isFollow()
    {
        $goodsId = input('goods_id');
        if (!$goodsId) {
            $this->error('参数缺失！');
        }

        $where = [
            'user_id' => $this->auth->id,
            'goods_id' => $goodsId,
            'goods_type' => 'score',
        ];

        $row_info = self::$model->where($where)->find();
        $flag = false;
        if ($row_info && $row_info->deletetime == 0 && $row_info->status == 'normal') {
            $flag = true;
        }

        $this->success('是否收藏', $flag);
    }
}
